package _0500_Keyboard_Row;

import _0000_study._common.Build;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * Date: 2021/9/16 9:55
 * Content:
 * 执行用时：1 ms, 在所有 Java 提交中击败了52.54%的用户
 * 内存消耗：36.6 MB, 在所有 Java 提交中击败了25.33%的用户
 */
public class Solution {

    public String[] findWords(String[] words) {
        String[] line = {
                "qwertyuiop",
                "asdfghjkl",
                "zxcvbnm"
        };
        HashMap<Character, Integer> hash = new HashMap<>();
        for (int i = 0; i < 3; i++)
            for (char c : line[i].toCharArray())
                hash.put(c, i);

        List<String> t = new ArrayList<>();
        for (String word : words) {
            HashSet<Integer> S = new HashSet<>();
            for (char c : word.toCharArray()) S.add(hash.get(Character.toLowerCase(c)));
            if (S.size() == 1) t.add(word);
        }
        String[] res = new String[t.size()];
        for (int i = 0; i < res.length; i++) res[i] = t.get(i);
        return res;
    }

    public static void main(String[] args) {

        String[] words = {"Hello", "Alaska", "Dad", "Peace"};
        System.out.println(Build.build((new Solution()).findWords(words)));
    }
}
